package com.speily.lock.framework;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

/**
 * <p>
 *    Redis 配置限流been
 * </p>
 *
 * @author SPL
 * @since 2019/12/30 18:05
 */
@Configuration
@EnableCaching
@Slf4j
public class RedisConfig {

    @Bean
    @SuppressWarnings("unchecked")
    public RedisScript<Long> limitRedisScript() {
        DefaultRedisScript redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/redis/limiter.lua")));
        redisScript.setResultType(Long.class);
        log.info("loading scripts/redis/limiter.lua finished!");
        return redisScript;
    }

}
